package com.chebaohui.iqs.service.impl.deal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.chebaohui.iqs.config.CompanyCode;
import com.chebaohui.iqs.config.ResultCode;
import com.chebaohui.iqs.entity.Paramter;
import com.chebaohui.iqs.entity.base.Cars;
import com.chebaohui.iqs.entity.base.Company;
import com.chebaohui.iqs.entity.req.CheckCar;
import com.chebaohui.iqs.entity.res.CompanyCars;
import com.chebaohui.iqs.service.impl.deal.bihu.BiHuCarThread;
import com.chebaohui.iqs.service.impl.deal.libao.LiBaoCarThread;
import com.chebaohui.iqs.service.impl.deal.test.TestCheck;
import com.chebaohui.iqs.service.impl.deal.util.CompanyUtil;

/**
 * 保险公司车辆查询
 * 
 * @author xielonghai
 *
 */
public class CarCheckCompanys {

	/**
	 * 查询
	 * 
	 * @param param
	 * @return
	 * @throws Exception
	 */
	public List<CompanyCars<List<Cars>>> check(Paramter<CheckCar> param) throws Exception {
		List<CompanyCars<List<Cars>>> res = new ArrayList<>();

		LiBaoCarThread lb = null;
		Map<String, Boolean> cCode = new HashMap<>();
		for (Company c : param.getData().getCompanys()) {
			CompanyCode code = CompanyUtil.dealCompany(c.getCompanyCode());
			if (code.isBh())
				cCode.put(c.getCompanyCode(), true);
			else if (code.getCode().equals(CompanyCode.LIBAO.getCode())) {
				lb = new LiBaoCarThread(param.getData());
				lb.start();
			} else if (code.getCode().equals(CompanyCode.TEST.getCode())) {
				res.add(TestCheck.getCar(param.getData(), code.getCode()));
			} else {
				CompanyCars<List<Cars>> carCheck = new CompanyCars<List<Cars>>();
				carCheck.setCompanyCode(code.getCode());
				carCheck.setCode(ResultCode.N03.getCode());
				carCheck.setMsg(ResultCode.N03.getMsg());
				res.add(carCheck);
			}
		}

		BiHuCarThread bh = null;
		if (!cCode.isEmpty()) {
			bh = new BiHuCarThread(param.getData(), cCode);
			bh.start();
		}

		if (null != lb) {
			res.add(lb.getResult());
		}

//		List<CompanyCars<List<Cars>>> carsListBh=bh.getCars();
		if (null != bh) {
			for (CompanyCars<List<Cars>> b : bh.getCars())
				res.add(b);
		}

		boolean check = true;
		for (CompanyCars<List<Cars>> r : res) {
			if (r.getCode().equals(ResultCode.SUCCESS.getCode())) {
				check = false;
				break;
			}
		}

		if (check)
			throw new Exception("车型查询失败");
		else
			return res;
	}
}
